﻿using System;
using System.Collections.ObjectModel;
using Microsoft.WowAddonStudio.FrameXml.Serialization.LayoutFrames;

namespace Microsoft.WowAddonStudio.FrameXml.Serialization.Support
{
    public class LayeredLayoutFrameList : Collection<LayeredLayoutFrame>
    {
        private readonly Layer owner;

        /// <summary>
        /// Initializes a new instance of the <see cref="LayeredLayoutFrameList"/> class.
        /// </summary>
        /// <param name="owner">The owner of the <see cref="LayeredLayoutFrameList"/>.</param>
        public LayeredLayoutFrameList(Layer owner)
        {
            if (owner == null)
                throw new ArgumentNullException("owner");
            
            this.owner = owner;
        }

        /// <summary>
        /// Inserts an element into the <see cref="T:System.Collections.ObjectModel.Collection`1"/> at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index at which <paramref name="item"/> should be inserted.</param>
        /// <param name="item">The object to insert. The value can be null for reference types.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">
        /// 	<paramref name="index"/> is less than zero.-or-<paramref name="index"/> is greater than <see cref="P:System.Collections.ObjectModel.Collection`1.Count"/>.</exception>
        protected override void InsertItem(int index, LayeredLayoutFrame item)
        {
            if (item == null)
                throw new ArgumentNullException("item");

            // Set the DrawLayer of the item when added to the layer
            item.DrawLayer = owner.Level;

            base.InsertItem(index, item);
        }
    }
}
